<?php

if (@txpinterface == 'admin') {
	add_privs('editlink','1,2,3,4,5,6');
  register_tab("extensions", "editlink", "Edit Link");
  register_callback("dub_admin_editlink", "editlink");
}

function dub_admin_editlink($event, $step) {

	global $dub_ae_cookie;
	include(txpath . '/include/txp_prefs.php');

	if (!isset($dub_ae_cookie)) {
		$dub_ae_cookie = "dub_article_edit";
		$rs = safe_insert('txp_prefs', "name='dub_ae_cookie', val='$dub_ae_cookie', prefs_id='1'");
	}

	if (gps("add")) {
		safe_update("txp_prefs", "val = '".addslashes(ps('dub_ae_cookie'))."'","name = 'dub_ae_cookie' and prefs_id ='1'");
    setcookie($dub_ae_cookie, $dub_ae_cookie, time()+31536000, "/");
		header("Location: index.php?event=editlink");
  } else if (gps("rem")) {
		safe_update("txp_prefs", "val = '".addslashes(ps('dub_ae_cookie'))."'","name = 'dub_ae_cookie' and prefs_id ='1'");
	  setcookie($dub_ae_cookie, $dub_ae_cookie, time()-3600, "/");
		header("Location: index.php?event=editlink");
  }

  pagetop("Edit Link");

	$aeset = isset($_COOKIE[$dub_ae_cookie]) ? "" : " not";

  $tdaStyle = ' style="text-align:right;vertical-align:middle"';
  echo form(startTable("list").
  tr(tdcs(hed("Add/Remove Public Site Article Edit Link",1),2)).
  tr(
  	tda(graf('Cookie '.$dub_ae_cookie.' is'.$aeset.' set.', ' align="center"'), ' colspan="2"')
  ).
  tr(
  	tda(gTxt('Cookie Name:'), ' style="text-align:right;vertical-align:middle"').tda(text_input("dub_ae_cookie",$dub_ae_cookie,'20'), ' ')
  ).
  tr(
  	tda(graf(fInput("submit","add",gTxt("Add Edit Link"),"publish").fInput("submit","rem",gTxt("Remove Edit Link"),"publish").eInput("editlink"), ' align="center"'), ' colspan="2"')
  ).
  endTable());

}

function dub_article_edit($atts,$thing="") {
	global $thisarticle, $dub_ae_cookie;

  extract(lAtts(array(
    'prefix' => '',
    'suffix' => ''
  ),$atts));

	return isset($_COOKIE[$dub_ae_cookie]) ? $prefix.'<a href="'.hu.'textpattern/index.php?event=article&amp;step=edit&amp;ID='.$thisarticle['thisid'].'">'.parse($thing).'</a>'.$suffix : "";
}

function dub_if_logged_in($atts, $thing) {
	global $dub_ae_cookie;
	
	$logged_in = isset($_COOKIE[$dub_ae_cookie]);
	
	/*
	if ( !empty($_COOKIE['txp_login']) ) {
		print "Got a cookie...";

		@list($c_userid,$cookie_hash) = split(',',cs('txp_login'));

		$nonce = safe_field('nonce','txp_users',"name='".doslash($c_userid)."'", 1);

		if ((md5($c_userid.$nonce) === $cookie_hash) && $nonce)  // check nonce
			$logged_in = true;	// cookie is good, create $txp_user
		else
			$logged_in = false;
	}
	else {
		print "No txp_login cookie...";
	}
	*/
	
	return parse( EvalElse($thing, $logged_in) );
}

?>
